package com.ydgk.service.impl;

import com.ydgk.bean.*;
import com.ydgk.dao.*;
import com.ydgk.service.HomeService;
import com.ydgk.vo.FurnitureInfo;
import com.ydgk.vo.HomeInfo;
import com.ydgk.vo.SalesInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private KindsMapper kindsMapper;

    @Autowired
    private KindsTypeMapper kindsTypeMapper;

    @Autowired
    private FurnitureMapper furnitureMapper;

    @Autowired
    private FurnitureKindsMapper furnitureKindsMapper;

    @Autowired
    private Pre_orderMapper pre_orderMapper;

    @Autowired
    private UserMapper userMapper;

    public HomeInfo getHomeInfo() {
        Float allSales = 0f;
        Float NProfit = 0f;
        Float LMProfit = 0f;
        List<Kinds> allKinds = kindsMapper.findAll();
        List<SalesInfo> salesInfoList = new ArrayList<SalesInfo>();
        for (Kinds kinds : allKinds) {
            Float kindsSales = 0f;
            List<FurnitureKinds> furnitureKindsList = furnitureKindsMapper.findByKid(kinds.getKid());
            for (FurnitureKinds  furnitureKinds : furnitureKindsList) {
               List<Pre_order> Pres = pre_orderMapper.getNPres(furnitureKinds.getFkid());
               int NCount = 0;
               for (Pre_order pre : Pres) {
                   NCount += pre.getCount();
               }
               kindsSales += furnitureKinds.getFkprice() * NCount;
               NProfit += furnitureKinds.getFkprice() * NCount - furnitureKinds.getCost() * NCount;

               List<Pre_order> LMPres = pre_orderMapper.getLMPres(furnitureKinds.getFkid());
               int LMCount = 0;
               for (Pre_order pre : LMPres) {
                   LMCount += pre.getCount();
               }
               LMProfit += furnitureKinds.getFkprice() * LMCount - furnitureKinds.getCost() * LMCount;
           }

            allSales += kindsSales;
            salesInfoList.add(new SalesInfo(kinds,kindsSales));
        }

        Map<String, Object> factorMap = new HashMap<String, Object>();
        factorMap.put("type","onSale");
        factorMap.put("start", 0);
        factorMap.put("size", 6);
        factorMap.put("sort", 5);
        List<Furniture> newFurnitures = furnitureMapper.findAll(factorMap);
        List<FurnitureInfo> newFurnitureInfos = new ArrayList<FurnitureInfo>();
        if(newFurnitures.size() > 0){
            for (Furniture furniture : newFurnitures){
                KindsType kindsType = kindsTypeMapper.findById(furniture.getKt_ktid());
                newFurnitureInfos.add(new FurnitureInfo(kindsType.getK_kid(),furniture));
            }
        }
        Integer NUser = userMapper.findNCount();
        Integer LMUser = userMapper.findLMCount();
        return new HomeInfo(allSales,salesInfoList,newFurnitureInfos,LMUser,NUser,LMProfit,NProfit);
    }
}
